package com.hqd.ch03.test.web.v39;


import com.hqd.ch03.v39.context.stereotype.Controller;
import com.hqd.ch03.v39.context.support.ResourceBundleMessageSource;
import com.hqd.ch03.v39.web.bind.annotation.RequestMapping;
import com.hqd.ch03.v39.web.ui.Model;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Locale;

@Controller
public class HelloController {
    private static final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();

    static {
        messageSource.getBasenameSet().add("messages");
    }

    @RequestMapping(value = "/index")
    public String index() {
        return "index";
    }

    @RequestMapping(value = "/getLogin")
    public String home(HttpServletRequest request, HttpServletResponse response,
                       Model model, Locale locale) throws UnsupportedEncodingException {
        String encoding = StandardCharsets.UTF_8.toString();
        request.setCharacterEncoding(encoding);
        response.setCharacterEncoding(encoding);
        // 获取国际化消息
        String message = messageSource.getMessage("welcome.message", null, locale);
        model.addAttribute("message", message);
        System.out.println(message);
        return "home";
    }
}
